<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Cache,Event;
class RoleController extends Controller
{
    protected $fields = [
        'name' => '',
        'desc' => '',
        'perms' => [],
    ];

    public function index(Request $request)
    {
        $res = DB::table('roles')->paginate(10);
        $count = DB::table('roles')->count();
        return view('admin.role.index', ['res' => $res,'count'=>$count]);
    }

    public function add(Request $request)
    {

        if($request->isMethod('post')){
            $data = Input::except('_token');
            $data['created_at'] = date('Y-m-d H:i:s',time());
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            DB::table('roles')->insert($data);
            return redirect('admin/role/index');
        }
        return view('admin.role.add');
    }

    public function edit($id,Request $request)
    {
        if($request->isMethod('post')){
            $obj = DB::table('roles');
            $data = Input::except('_token','permissions','allChecked');
            //修改权限
            if(Input::has('permissions')){
                $arr = Input::get('permissions');
                $permission_id = '';
                foreach ($arr as $v => $k) {
                    $permission_id .= $k . ',';
                }
                $data['permission_id'] = substr($permission_id, 0, -1);
            }
            $data['updated_at'] = date('Y-m-d H:i:s',time());
            $obj->where('id',$id)->update($data);
            return redirect('admin/role/index');
        }
        $re = DB::table('roles')->where('id',$id)->first();
        $re->permission_id = explode(",",$re->permission_id);

        //权限列表
        $yiji = DB::table('permissions')->where('cid','0')->orderby('paixu')->orderby('id')->get();
        $erji = DB::table('permissions')->where('cid','>','0')->orderby('paixu')->orderby('id')->get();
        return view('admin.role.edit', ['re' => $re,'yiji'=>$yiji,'erji'=>$erji]);
    }

    public function del($id)
    {
        $tag = Role::find((int)$id);
        if ($tag) {
            DB::table('roles')->where('id',$id)->delete();
            return redirect()->back();
        } else {
            echo "<script>alert('删除失败!');window.history.go(-1);</script>";
        }
    }

    public function cate_tree($arr,$id,$level=0){
        $sub = array();
        foreach($arr as $key => $value){
            if ($value->cid == $id){
                $value->level = $level + 1;
                $sub[] = $value;
                $sub = array_merge($sub,$this->cate_tree($arr,$value->id,$level+1));
            }
        }
        return $sub;
    }

    public function catelist(&$catelist){
        foreach($catelist as $k => $v){
            if($v->level != 1){
                $catelist[$k]->name = str_repeat('&nbsp;&nbsp;',$v->level).'|-&nbsp;&nbsp;'.$v->name;
            }
        }
    }
}
